import hashlib
import json
import os
import re
import urllib
import urllib.request

from arcface.struct_info import ASF_FaceFeature


def loadFaceFeature(datalist):
    listData = []
    for p in datalist:
        d = loadData(p)
        listData.append(d)
    return listData
def loadFeaturesMap(datalist):
    listData = []
    for p in datalist:
        data = {}
        data["id"]=getFileName(p)
        data['feature']=loadData(p)
        listData.append(data)
    return listData
#扫描目标文件下xx后缀的文件并返回列表
def scanpath(filepath, suffix):
    filelist = []
    dataList = []
    #print("开始扫描【{0}】".format(filepath))
    if not os.path.isdir(filepath):
        print("【{0}】不是目录".format(filepath))
        return filelist,dataList
    for filename in os.listdir(filepath):
        if os.path.isdir(filepath + "/" + filename):
            filelist.extend(scanpath(filepath + "/" + filename, suffix))
        else:
            if filename.endswith(suffix):
                filelist.append((getFileName(filename)))
                realpath = filepath + "/" + filename
                #print("文件路径", realpath)
                #data1 = loadData(realpath)
                #print(data1.feature)
                dataList.append(realpath)
    return filelist,dataList
def scanAllfilepath(dataList,filepath):
    #print("开始扫描【{0}】".format(filepath))
    if not os.path.isdir(filepath):
        print("【{0}】不是目录".format(filepath))
        return dataList
    for filename in os.listdir(filepath):
        if os.path.isdir(filepath + "/" + filename):
            scanAllfilepath(dataList,filepath + "/" + filename)
        else:
            realpath = filepath + "/" + filename
            dataList.append(realpath)


def scanpathDeep(filelist,filepath, suffix):
    if not os.path.isdir(filepath):
        print("【{0}】不是目录".format(filepath))
        exit(-1)
    for filename in os.listdir(filepath):
        if os.path.isdir(filepath + "/" + filename):
            #filelist.extend(scanpathDeep(filelist,filepath + "/" + filename, suffix))
            scanpathDeep(filelist, filepath + "/" + filename, suffix)
        else:
            if filename.endswith(suffix):
                filelist.append((getFileName(filename)))
                realpath = filepath + "/" + filename
                print("文件路径", realpath)
                #data1 = loadData(realpath)
                #print(data1.feature)
                #dataList.append(realpath)
    #return filelist#,dataList

    pass
def scanpathdir(filelist,filepath):
    if not os.path.isdir(filepath):
        print("【{0}】不是目录".format(filepath))
        return
    for filename in os.listdir(filepath):
        if os.path.isdir(filepath + "/" + filename):
            filelist.append(filename)
        else:
            continue

    pass
#判断输入是否是图片
def isImgorFileDir(source):
    extensions = {".jpg", ".png", ".gif"}
    if os.path.isdir(source):
        return 0
    elif (source.endwith(ext) for ext in extensions):
        return 1
    else:
        return -1


def scanpathByRe(filepath, suffix):
    filelist = []
    dataList = []
    print("开始扫描【{0}】".format(filepath))
    if not os.path.isdir(filepath):
        print("【{0}】不是目录".format(filepath))
        exit(-1)
    for filename in os.listdir(filepath):
        if os.path.isdir(filepath + "/" + filename):
            filelist.extend(scanpath(filepath + "/" + filename, suffix))
        else:
            print("scanpathByRe is",filename,re.search(suffix,filename))
            if re.search(suffix,filename):
                filelist.append((getFileName(filename)))
                realpath = filepath + "/" + filename
                print("文件路径", realpath)
                #data1 = loadData(realpath)
                #print(data1.feature)
                dataList.append(realpath)
    return filelist,dataList
# 查找视频文件
def scanpathBydeepRe(dataList,filepath, suffix):
    #filelist = []
    #dataList = []
    print("开始扫描【{0}】".format(filepath))
    if not os.path.isdir(filepath):
        print("【{0}】不是目录".format(filepath))
        exit(-1)
    for filename in os.listdir(filepath):
        if os.path.isdir(filepath + "/" + filename):
            scanpathBydeepRe(dataList,filepath + "/" + filename, suffix)
        else:
            print("scanpathByRe is",filename,re.search(suffix,filename))
            if re.search(suffix,filename):
                #filelist.append((getFileName(filename)))
                realpath = filepath + "/" + filename
                print("文件路径", realpath)
                #data1 = loadData(realpath)
                #print(data1.feature)
                dataList.append(realpath)
    return dataList

'''
截取文件名
'''
def getFileName(source):
    print(source)
    path = str(source).rfind('/')
    print("getFileName is:",source[path:])
    print("getFileName is:",source[path+1:])
    namefile = str(source[path+1:]).split(".")
    print("文件名称",namefile[0])
    return namefile[0]

def loadData(path):
    data = ASF_FaceFeature()
    content = b''
    #print("文件路径加载",path)
    if os.path.exists(path):
        print("文件存在",path)
    with open(path, 'rb') as file:
        lines = file.readlines()
        #print("读取长度", len(lines))
        for line in lines:  # 依次读取每行
            content += line#.strip()  # 去掉每行头尾空白
            #print("读取的数据为: %s" % (content))
        #print("read file",lines)
        file.close()
    #print("类型",type(lines))
    #print(content)

    #for l in lines:
        #print("type",type(l))
    #arface = str.encode(lines)
    data.featureSize = 1032
    #print(type(content))
    data.set_feature(content, 1032)
    #print("读取文件内存长度", data.featureSize)
    #print("读取文件内存", data.feature)
    return data


def mkdir(path):
    # 引入模块
    import os

    # 去除首位空格
    path = path.strip()
    # 去除尾部 \ 符号
    path = path.rstrip("\\")

    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(path)

    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path)

        print(path + ' 创建成功')

        return True
    else:
        # 如果目录存在则不创建，并提示目录已存在
        print(path + ' 目录已存在')

        return False
'''
filepath:生成的图片路径
file_name:文件名称
img_url:文件链接
'''
def downloadFile(filepath,file_name,img_url):
    print("downloadFile filepath ",filepath, file_name,img_url)
    try:
        # 是否有这个路径
        if not os.path.exists(filepath):
            # 创建路径
            os.makedirs(filepath)
        # 获得图片后缀
        file_suffix = os.path.splitext(img_url)[1]
        print(file_suffix)
        # 拼接图片名（包含路径）
        #filename = '{}{}{}{}'.format(filepath, os.sep, file_name, file_suffix)
        filename = '{}{}{}'.format(filepath, os.sep, file_name)
        print(filename)
        # 下载图片，并保存到文件夹中
        urllib.request.urlretrieve(img_url, filename=filename)

    except IOError as e:
        print("IOError",e)
    except Exception as e:
        print("Exception", e)
def GetFileMd5(filename,url):

    if not os.path.isfile(filename):
        return
    myhash = hashlib.md5()
    f = open(filename,'rb')
    while True:
        b = f.read(8096)
        if not b :
            break
        myhash.update(b)
    f.close()
    return myhash.hexdigest()
#
def getDefaultExt(self,type):
    if type == 'video' or type == 'bodyKeyPointInput' or type == 'archiveVideo':
        return '.mp4'
    elif type == 'faceResult' or type == 'bodyKeyPointResult':
        return '.json'
    else:
        return ''

#获取文件名称的后缀
def getExtFile(path):
    extName = os.path.splitext(path)[-1]
    return extName
#获取文件名称
def getFileName(path):
    name = os.path.basename(path)
    return name
#获取文件夹目录
def getFilePath(path):
    l = os.path.dirname(path)
    return l
#获取文件前缀名称 test。html 返回test
def getFilepreName(path):
    return os.path.splitext(path)[0]

def cleanDir(filepath):
    print("cleanDir is ",filepath)
    for filename in os.listdir(filepath):
        if os.path.isdir(filepath + "/" + filename):
            cleanDir(filepath + "/" + filename)
        else:
            print("cleanDir is ",filename)
            os.remove(filepath+"/"+filename)

    #for filename in os.listdir(filepath):
        #os.rmdir(filepath + "/" + filename)
    pass
# 删除该目录下所有的文件及目录
def cleanFile(path):
    for root, dirs, files in os.walk(path, topdown=False):
        for name in files:
            os.remove(os.path.join(root, name))
        for name in dirs:
            print(name,type(name))
            os.rmdir(os.path.join(root, name))

    pass

# jsonpath 输出文件路径 ,strjson 输出文件内容
def writeJsonFile(jsonpath,strjson):
    print(jsonpath)
    f = open(jsonpath, 'w',encoding='utf-8')
    f.write(json.dumps(strjson,ensure_ascii=False))
    f.close()
    pass

if __name__ == '__main__':
    path = "C:\\test\\class\\feature"
    suffix = "arcface"
    #filelist = fileListFunc(path)
    filelist,datalist = scanpath(path,suffix)
    print(len(filelist))
    print(filelist)
    print(datalist)
    listData = []
    for p in datalist:
        d = loadData(p)
        listData.append(d)
    print(listData.__sizeof__())
    print()